package cn.xfdzx.android.apps.shop.activity.myshop;

import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.adapter.CreateZhiBoAdapter;
import cn.xfdzx.android.apps.shop.adapter.XFDProductAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.LiveInfoBeen;
import cn.xfdzx.android.apps.shop.bean.PushInfoBeen;
import cn.xfdzx.android.apps.shop.contract.CreateZhiboContract;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.CreateZhiboPresenter;
import cn.xfdzx.android.apps.shop.presenter.ShopManagerPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.util.Utils;

public class CreateZhiBoActivity extends BaseMvpActivity<CreateZhiboPresenter> implements CreateZhiboContract.View {

    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.tv_create)
    TextView tvCreate;
    @BindView(R.id.tv_yijian)
    TextView tvYijian;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.tv_zhibo_id)
    TextView tvZhiboId;
    @BindView(R.id.ll_state)
    LinearLayout llState;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.tv_copy)
    TextView tvCopy;
    private RelativeLayout.LayoutParams item_bar;

    private CreateZhiBoAdapter createZhiBoAdapter;
    private String imageUrl;//店铺logo
    private String name;//店铺name
    private int roomId;//直播间id
    private List<LiveInfoBeen.DataBean.LiveListBean> historyList=new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_create_zhi_bo;
    }

    @Override
    public void initView() {
        initHigh();
        StatusBarUtil.setLightStatusBar(this, false, true);
        mPresenter = new CreateZhiboPresenter();
        mPresenter.attachView(this);



        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvList.setLayoutManager(linearLayoutManager);
        createZhiBoAdapter = new CreateZhiBoAdapter(this,historyList);

        rvList.setAdapter(createZhiBoAdapter);
    }

    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }

    @Override
    public void showLoading() {
        if (EmptyUtil.isNotEmpty(ProgressDialog.getInstance().materialDialog)){
            if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
                ProgressDialog.getInstance().show(this);
            }
        }

    }

    @Override
    public void hideLoading() {

            ProgressDialog.getInstance().dismiss();

    }

    @Override
    public void onError(Throwable throwable) {

    }


    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.liveInfo();
    }

    @Override
    public void onLiveInfoSuccess(LiveInfoBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            imageUrl=bean.getData().getLive().getCover();
            roomId = bean.getData().getLive().getId();
            RoundedCorners roundedCorners = new RoundedCorners(20);
            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
            options.placeholder(R.drawable.icon_cover)//图片加载出来前，显示的图片
                    .fallback(R.drawable.icon_cover) //url为空的时候,显示的图片
                    .error(R.drawable.icon_cover);;
            Glide.with(this).load(getIntent().getStringExtra("image")).apply(options.transform(new CircleCrop(), roundedCorners)).into(ivLogo);
            name=bean.getData().getLive().getTitle();
            tvShopName.setText(getIntent().getStringExtra("name"));
            tvZhiboId.setText("直播ID：" + bean.getData().getLive().getId());
            historyList.clear();
            historyList.addAll(bean.getData().getLive_list());
            createZhiBoAdapter.notifyDataSetChanged();
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onPushInfoSuccess(PushInfoBeen bean,boolean is_yijian) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            Intent intent = new Intent(this, CreatePusherActivity.class);
            intent.putExtra("image", imageUrl);
            intent.putExtra("video_url", bean.getData().getPushUrl());
            intent.putExtra("name", name);
            intent.putExtra("yijian", is_yijian);
            intent.putExtra("roomId", roomId);

            startActivity(intent);
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_create, R.id.tv_yijian,R.id.tv_copy})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_copy:
                Utils.copy(this, tvCopy.getText().toString());
                Toast.makeText(this, "复制成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_create:
                if (checkPublishPermission()) {
                    // 检查权限
                    mPresenter.pushInfo(false);
                }
                break;
            case R.id.tv_yijian:
                if (checkPublishPermission()) {
                    mPresenter.pushInfo(true);
                }
                break;
        }
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }


    private boolean checkPublishPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            List<String> permissions = new ArrayList<>();
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)) {
                permissions.add(Manifest.permission.CAMERA);
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)) {
                permissions.add(Manifest.permission.RECORD_AUDIO);
            }
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)) {
                permissions.add(Manifest.permission.READ_PHONE_STATE);
            }
            if (permissions.size() != 0) {
                ActivityCompat.requestPermissions(this,
                        permissions.toArray(new String[0]),
                        100);
                return false;
            }
        }
        return true;
    }
}
